package SparseArray;

//五子棋棋盘
public class ChessBoard {
    //行长度（列数）
    private int row;
    //列长度（行数）
    private int col;
    //棋子数量
    private int pieceCount = 0;
    //棋盘
    private int[][] chessboard;

    public ChessBoard(int row, int col) {
        this.row = row;
        this.col = col;
        this.chessboard = new int[row][col];
    }

    /**
     * 增加黑子（定义黑子为1）
     *
     * @param row int X轴位置
     * @param col int Y轴位置
     *
     * @return void
     *
     * @throws RuntimeException
     **/
    public void addBlack(int row, int col) {
        int piece = 1;
        this.putPiece(row, col, piece);
    }

    /**
     * 增加白子（定义白子为2）
     *
     * @param row int X轴位置
     * @param col int Y轴位置
     *
     * @return void
     *
     * @throws RuntimeException
     **/
    public void addWhite(int row, int col) {
        int piece = 2;
        this.putPiece(row, col, piece);
    }

    /**
     * 棋盘中，落一个子
     *
     * @param row   int X轴位置
     * @param col   int Y轴位置
     * @param piece int 棋子标记（1代表黑子，2代表白子）
     *
     * @return void
     *
     * @throws RuntimeException
     **/
    protected void putPiece(int row, int col, int piece) {
        if (this.pieceCount >= this.row * this.col) {
            throw new RuntimeException("棋盘中棋子已满");
        }

        if (1 != piece && 2 != piece) {
            throw new RuntimeException("棋子标记格式有误");
        }

        if (row > this.row) {
            throw new RuntimeException("不符合规范的列号");
        }

        if (col > this.col) {
            throw new RuntimeException("不符合规范的行号");
        }

        if (0 != this.chessboard[row][col]) {
            throw new RuntimeException("此处位置已经被占用");
        }

        this.chessboard[row][col] = piece;
        this.pieceCount++;
    }

    /**
     * 打印棋盘
     *
     * @return void
     **/
    public void print() {
        for (int[] rowItem : this.chessboard) {
            for (int item : rowItem) {
                System.out.printf("%d \t", item);
            }
            System.out.println();
        }
    }

    /**
     * 导出稀疏数组
     *
     * @return int[][]
     **/
    public int[][] toSparseArray() {
        int[][] sparseArray = new int[this.pieceCount + 1][3];
        //整理稀疏数组
        sparseArray[0][0] = this.col;
        sparseArray[0][1] = this.row;
        sparseArray[0][2] = this.pieceCount;

        int index = 1;
        for (int row = 0; row < this.row; row++) {
            for (int col = 0; col < this.col; col++) {
                if (0 != this.chessboard[row][col]) {
                    sparseArray[index][0] = row;
                    sparseArray[index][1] = col;
                    sparseArray[index][2] = this.chessboard[row][col];
                    index++;
                }
            }
        }

        return sparseArray;
    }

    /**
     * 打印稀疏数组
     *
     * @return void
     **/
    public void printSparseArray() {
        int[][] sparseArray = this.toSparseArray();
        for (int[] items : sparseArray) {
            for (int item : items) {
                System.out.printf("%d \t", item);
            }
            System.out.println();
        }
    }
}
